package com.cc.controller;


import com.cc.model.EbProduct;
import com.cc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
        @Autowired
        private ProductService productService;
        @RequestMapping("/list")
        public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,
                                 @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
            //获取新闻表的第一页数据，每页三条
            List<EbProduct> productes=productService.list(page,pageSize);
            int maxPage = productService.getMaxPage(pageSize);
            //将数据（list）和视图（jsp）返回给中央控制器spring mvc
            ModelAndView mv=new ModelAndView();
            //绑定数据
            mv.addObject("productes",productes);//key-value
            //绑定当前页
            mv.addObject("curPage",page);
            //绑定最大页
            mv.addObject("maxPage",maxPage);
            //绑定视图
            mv.setViewName("manage/product");
            return mv;

        }

    @RequestMapping("/to_save")
    public String toSave(){
        return "manage/product-add";
    }
    @RequestMapping("/save")
    public String save(EbProduct ebProduct, MultipartFile pictrueFile, HttpServletRequest request){
        System.out.println("进入ProductController.save==="+ebProduct+"\tpic=="+pictrueFile);
        //1.将数据添加到数据库(调用service)
        //1.1. 参数校验
        if((ebProduct.getEpName()==null||
                ebProduct.getEpName().equals("")||
                ebProduct.getEpName().trim().length()==0)||
        (      ebProduct.getEpFileName()==null||
                ebProduct.getEpFileName().equals("")||
                ebProduct.getEpFileName().trim().length()==0))
        {
            return "manage/manage-error";//error.jsp
        }
        //1.2 将商品的信息保存到数据
        //1.2.1 设置商品的图片的名字
        String originFileName = pictrueFile.getOriginalFilename();
        ebProduct.setEpFileName(originFileName);

        int result = productService.save(ebProduct);
        //1.3 文件上传（图片从客户端的电脑上上传到服务器(tomcat  images/products) ）
        //1.3.1指定上传到服务器的哪个路劲
        String rootPath = request.getServletContext().getRealPath("/");
        System.out.println("root:"+rootPath);
        String filePath = rootPath+"WEB-INF/images/product/"+originFileName;
        //创建文件
        File file =new File(filePath);
        if(!file.exists()){
            file.mkdir();
        }
        //1.3.2 上传
        try {
            pictrueFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("上传文件成功！！");
        //2.将成功的视图发送给 spring mvc的中央控制器

        return "manage/manage-result";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        //1.参数校验

        //2.执行删除
        int result = productService.deleteByPk(id);
        if(result<=0){
            //TODO 要么return错误页面 要么抛出异常
            return "manage/product-error";
        }
        return "manage/product-result";
    }


    @RequestMapping("/edit")
    public String edit(Model model, Integer epId){
        EbProduct ebProduct=productService.selectByCondition(epId);
        model.addAttribute("ebProduct",ebProduct);
        return  "manage/product-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbProduct ebProduct){


        //1.更新新闻信息
       int result =productService.updateById(ebProduct);
        //2.跳转到更新结果页面
        if(result <= 0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    }

